package com.agree;

import java.awt.Frame;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import com.agree.parse.ParseData;

public class MyFrame extends Frame{  
	private OutputStream outputStream = null;
	ParseData ps = new ParseData();
	private static final long serialVersionUID = 1L;

	public MyFrame() {  
  
		// 默认的窗体名称  
        this.setTitle("显示一张图片");
  
        // 获得面板的实例  
        MyPanel mp = new MyPanel(); 
    
        mp = new MyPanel();
        
    	new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
	//				revData();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}).start();
			
		new Thread(new Runnable() {
					
			@Override
			public void run() {
				try {
					sendData();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}).start();
    	
        mp.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
            	
            }

            @Override
            public void mousePressed(MouseEvent e) {

                System.out.println("i press"+e.getX()+","+e.getY());
                Point point = pointConvert(e.getPoint());
                if (outputStream != null) {
                    String command = String.format("d 0 %s %s 50\n", (int)point.getX(), (int)point.getY());
                    executeTouch(command);
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("i release");
                if (outputStream != null) {
                    String command =  "u 0\n";
                    executeTouch(command);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

            @Override
            public void mouseExited(MouseEvent e) {

            }
        });


        mp.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {
                System.out.println(e.getPoint().getX()+","+e.getPoint().getY());
                Point point = pointConvert(e.getPoint());
                if (outputStream != null) {
                    String command = String.format("m 0 %s %s 50\n", (int)point.getX(), (int)point.getY());
                    executeTouch(command);
                }
            }

            @Override
            public void mouseMoved(MouseEvent e) {

            }
        });
        
        
        this.add(mp);  
        this.addWindowListener(new WindowAdapter() {  
            //设置关闭  
            public void windowClosing(WindowEvent e) {  
                System.exit(0);  
            }  
        });  
         
        // 执行并构建窗体设定  
        this.pack();  
        this.setVisible(true);  
        
        new Thread(new Runnable() {
			
    		@Override
    		public void run() {
    			
    			ps.server();				
    		}
    		}).start();
        
    }  

	  private int width = 440;
	  private int height = 900;
	  private int oriWidth = 1080;
	  private int oriHight = 2340;
	 private Point pointConvert(Point point)
	    {
	        Point realpoint = new Point((int)((point.getX()*1.0 / width) * oriWidth) , (int)((point.getY()*1.0 /height) * oriHight));
	        return realpoint;
	    }

	    private void executeTouch(String command) {
	        if (outputStream != null) {
	            try {
	            	outputStream = socket.getOutputStream();
	                System.out.println("command" + command);
	                outputStream.write(command.getBytes());
	                outputStream.flush();
	                String endCommand = "c\n";
	                outputStream.write(endCommand.getBytes());
	                outputStream.flush();
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
	    }
	 
	  private Socket socket;
	  
	  
	  private void revData()throws Exception {
		  Socket  sk = new Socket();
		  SocketAddress socketAddress = new InetSocketAddress("127.0.0.1",1090);
		  sk.connect(socketAddress);
		  InputStream stream = sk.getInputStream();
  //        outputStream = sk.getOutputStream();
          int len = 4096;

          byte[] buffer;
          buffer = new byte[len];
          while(true) {
	          int realLen = stream.read(buffer);
	          if(realLen == -1) {
	        	  break ;
	          }
	         
	          if (buffer.length != realLen) {
	              buffer = subByteArray(buffer, 0, realLen);
	              System.out.println(buffer);
	          }
          }
          sk.close();
	  }
	  
	  private void sendData()throws Exception {
		  socket = new Socket();
		  SocketAddress socketAddress = new InetSocketAddress("127.0.0.1",1111);
		  socket.connect(socketAddress);
		  InputStream stream = socket.getInputStream();
          outputStream = socket.getOutputStream();
          int len = 4096;

          byte[] buffer;
          buffer = new byte[len];
          int realLen = stream.read(buffer);
          if(realLen == -1) {
        	  return ;
          }
         
          if (buffer.length != realLen) {
              buffer = subByteArray(buffer, 0, realLen);
          }
          String result = new String(buffer);
          String array[] = result.split(" |\n");
          System.out.println("hhe");
	  }
	  
	  private byte[] subByteArray(byte[] byte1, int start, int end) {
          byte[] byte2 = new byte[0];
          try {
              byte2 = new byte[end - start];
          } catch (NegativeArraySizeException e) {
              e.printStackTrace();
          }
          System.arraycopy(byte1, start, byte2, 0, end - start);
          return byte2;
      }
}
